package com.sunday.aomicaichang.ui.recharge;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sunday.common.adapter.CommonAdapter;
import com.sunday.common.adapter.RecylerViewHolder;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.aomicaichang.R;
import com.sunday.aomicaichang.http.ApiClient;
import com.sunday.aomicaichang.ui.common.WebViewActivity;
import com.sunday.aomicaichang.ui.order.PayConfirmActivity;
import com.sunday.member.entity.RechargeItem;

import com.sunday.member.entity.order.OrderItem;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/6/8.
 *
 * 会员充值
 */

public class SelectRechargeActivity extends BaseActivity {
    @Bind(R.id.share)
    TextView share;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    private LinearLayoutManager layoutManager;
    private CommonAdapter<RechargeItem> adapter;
    private List<RechargeItem> dataSet=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_recharge);
        ButterKnife.bind(this);
        initRecyclerView();
        getData();
    }

    private void initRecyclerView(){
        layoutManager=new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        adapter=new CommonAdapter<RechargeItem>(mContext,R.layout.item_member_recharge,dataSet) {
            @Override
            public void convert(RecylerViewHolder holder, RechargeItem rechargeItem) {
                bind(holder,rechargeItem);
            }
        };
        recyclerView.setAdapter(adapter);
    }

    private void bind(RecylerViewHolder holder, final RechargeItem item){
//        ((TextView)holder.getView(R.id.cp_money)).setText(String.format("%s",item.getBaseAmount().setScale(0, RoundingMode.HALF_UP)));
//        ((TextView)holder.getView(R.id.cp_tip)).setText(String.format("充值%s",item.getBaseAmount().setScale(0, RoundingMode.HALF_UP))+"  "+String.format("送%s",item.getFreeAmount().setScale(2, RoundingMode.HALF_UP)));

        ((TextView)holder.getView(R.id.cp_money)).setText(String.valueOf(item.getBaseAmount()));
        ((TextView)holder.getView(R.id.cp_tip)).setText(String.valueOf(item.getBaseAmount()+"  送"+String.valueOf(item.getFreeAmount())));
        LinearLayout lyCoupon = holder.getView(R.id.ly_coupon);
        if(item.isSelect()){
            lyCoupon.setSelected(true);
        }else {
            lyCoupon.setSelected(false);
        }
        holder.setOnClickListener(R.id.ly_coupon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(item.isSelect()) {
                    createRechargeOrder(item.getId());
                }else {
                    for(int i=0;i<dataSet.size();i++){
                        dataSet.get(i).setSelect(false);
                    }
                    item.setSelect(true);
                    adapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void createRechargeOrder(int id){
        String nonce=MD5Utils.random();
        String[] str1=new String[]{"productId","nonce"};
        String[] str2=new String[]{String.valueOf(id),nonce};
        showLoadingDialog(0);
        Call<ResultDO<OrderItem>>call= MemberClient.getApiAdapter().createRechargeOrder(id,nonce,SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO<OrderItem>>() {
            @Override
            public void onResponse(Call<ResultDO<OrderItem>> call, Response<ResultDO<OrderItem>> response) {
                if (isFinish||response.body()==null){return;}
                dissMissDialog();
                if (response.body().getCode()==0){
                    intent=new Intent(mContext,PayConfirmActivity.class);
                    intent.putExtra("order",response.body().getResult());
                    intent.putExtra("type",2);
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(Call<ResultDO<OrderItem>> call, Throwable t) {
                dissMissDialog();
            }
        });
    }

    private void getData(){
        String nonce= MD5Utils.random();
        String[] str1=new String[]{"nonce"};
        String[] str2=new String[]{nonce};
        Call<ResultDO<List<RechargeItem>>> call= MemberClient.getApiAdapter().getRechargeList(nonce,SignUtil.getSign(str1,str2));
        call.enqueue(new Callback<ResultDO<List<RechargeItem>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<RechargeItem>>> call, Response<ResultDO<List<RechargeItem>>> response) {
                if (isFinish||response.body()==null){return;}
                if (response.body().getCode()==0){
                    dataSet.clear();
                    dataSet.addAll(response.body().getResult());
                    if(dataSet!=null&&dataSet.size()>0)
                    for(int i=0;i<dataSet.size();i++){
                        dataSet.get(i).setSelect(false);
                    }
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<RechargeItem>>> call, Throwable t) {

            }
        });
    }



    @OnClick(R.id.share)
    void onClick(){
        intent = new Intent(mContext, WebViewActivity.class);
        intent.putExtra("url", ApiClient.API_URL1+"/sunday/mobile/advertisement/html5ByZoneCode?zoneKey=income_rule" );
        intent.putExtra("title","会员充值说明");
        startActivity(intent);
    }


}
